require(['validator', 'date'], function () {
    var $right = $("#alarmSettingArticle"),
        corpId = "no";
    var $sysSettingForm = $('#sysSettingForm');
    $sysSettingForm.initForm();
    // 左侧树
    commonFun.leftTreeNodeClickObj['sysAlarmCorpIdSet'] = function (event, treeId, treeNode) {
        corpId = treeNode.corpId;
        if (treeNode.level == 0) {
            corpId = "";
        }
        $("#corpId").val(corpId);
        initRight();
    };

    $(window).on('resize', function () {
    	$right.height($(window).height() - 67);
    }).trigger('resize');

    var initRight = function () {
    	var node = commonFun.getLeftTreeSelectedNodes();
    	if(node && node.length > 0){
    		corpId = node[0].corpId;
    		$.ajaxExt({
    			async: false,
    			url: "/alarmPushSetting/searchAlarmTimeSetting.htm",
    			data: {"corpId": corpId}
    		}).done(function (data) {
    			for(var i=1;i<4;i++){
    	    		$("#timeStart"+i).val("");
    	    		$("#timeEnd"+i).val("");
    	    		$("#mon"+i+", #tues"+i+", #wed"+i+", #thur"+i+", #fri"+i+", #sat"+i+", #sun"+i).prop("checked", false);
    	    	}
    			if (data && data[0]) {
    				if(data[0].timeStart1) $("#timeStart1").val(data[0].timeStart1);
    				if(data[0].timeEnd1) $("#timeEnd1").val(data[0].timeEnd1);
    				if(data[0].timeStart2) $("#timeStart2").val(data[0].timeStart2);
    				if(data[0].timeEnd2) $("#timeEnd2").val(data[0].timeEnd2);
    				if(data[0].timeStart3) $("#timeStart3").val(data[0].timeStart3);
    				if(data[0].timeEnd3) $("#timeEnd3").val(data[0].timeEnd3);
    				for(var i=1;i<4;i++){
    					var json = data[0];
    					if(json.hasOwnProperty("timeWeek"+i)){
    						var timeWeekStr = json["timeWeek"+i];
    						if(timeWeekStr.charAt(0)==1) $("#mon"+i).prop("checked", true);
    						if(timeWeekStr.charAt(1)==1) $("#tues"+i).prop("checked", true);
    						if(timeWeekStr.charAt(2)==1) $("#wed"+i).prop("checked", true);
    						if(timeWeekStr.charAt(3)==1) $("#thur"+i).prop("checked", true);
    						if(timeWeekStr.charAt(4)==1) $("#fri"+i).prop("checked", true);
    						if(timeWeekStr.charAt(5)==1) $("#sat"+i).prop("checked", true);
    						if(timeWeekStr.charAt(6)==1) $("#sun"+i).prop("checked", true);
    					}
    				}
    			} 
    		});
    		
//    		$(".week1").each(function(){
//        		if($(this).is(':checked')){
//        			console.info(true);
//        		}
//        	});
//    		console.info($(".week1").is(':checked'));
    	}
    };

    $("#saveSetting").on("click", function () { // 保存
    	if(corpId=='no' || corpId == ''){
    		$.messager.alert("提示", "请选择左侧树企业。", "info");
    		return;
    	}
        var formVal = $sysSettingForm.getValues();
        formVal.corpId = corpId;
        
        if($("#timeStart1").val()=='' && $("#timeEnd1").val()=='' && $("#timeStart2").val()==''
        	&& $("#timeEnd2").val()=='' && $("#timeStart3").val()=='' && $("#timeEnd3").val()==''){
        	$.messager.alert("提示", "请填写至少一组开始结束时间。", "info");
        	return;
        }
        
        if($("#timeStart1").val()!="" && $("#timeEnd1").val() != "" && $("#timeStart1").val() > $("#timeEnd1").val()){
        	$.messager.alert("提示", "开始时间不能超过结束时间。", "info");
        	return;
        }
        if($("#timeStart2").val()!="" && $("#timeEnd2").val() != "" && $("#timeStart2").val() > $("#timeEnd2").val()){
        	$.messager.alert("提示", "开始时间不能超过结束时间。", "info");
        	return;
        }
        if($("#timeStart3").val()!="" && $("#timeEnd3").val() != "" && $("#timeStart3").val() > $("#timeEnd3").val()){
        	$.messager.alert("提示", "开始时间不能超过结束时间。", "info");
        	return;
        }
        
        if(($("#timeStart1").val()=='' && $("#timeEnd1").val()!='') || 
        		($("#timeStart2").val()=='' && $("#timeEnd2").val()!='') || 
        		($("#timeStart3").val()=='' && $("#timeEnd3").val()!='')){
        	$.messager.alert("提示", "开始时间不能为空。", "info");
        	return;
        }
        
        if(($("#timeStart1").val()!='' && $("#timeEnd1").val()=='') || 
        		($("#timeStart2").val()!='' && $("#timeEnd2").val()=='') || 
        		($("#timeStart3").val()!='' && $("#timeEnd3").val()=='')){
        	$.messager.alert("提示", "结束时间不能为空。", "info");
        	return;
        }
        
        if(($("#timeStart1").val()!='' && $("#timeEnd1").val()!='' && !$(".week1").is(':checked')) 
            	|| ($("#timeStart2").val()!='' && $("#timeEnd2").val()!='' && !$(".week2").is(':checked'))
            	|| ($("#timeStart3").val()!='' && $("#timeEnd3").val()!='' && !$(".week3").is(':checked'))){
            $.messager.alert("提示", "请选择星期。", "info");
           	return;
        }
        
        if(($("#timeStart1").val()=='' && $("#timeEnd1").val()=='' && $(".week1").is(':checked')) 
            	|| ($("#timeStart2").val()=='' && $("#timeEnd2").val()=='' && $(".week2").is(':checked'))
            	|| ($("#timeStart3").val()=='' && $("#timeEnd3").val()=='' && $(".week3").is(':checked'))){
            $.messager.alert("提示", "请选择开始和结束时间。", "info");
           	return;
        }
        
        for(var i=1;i<4;i++){
        	var timeWeek = ($("#mon"+i).is(':checked')?"1":"0") + ($("#tues"+i).is(':checked')?"1":"0") 
        		+ ($("#wed"+i).is(':checked')?"1":"0") + ($("#thur"+i).is(':checked')?"1":"0")
        		+ ($("#fri"+i).is(':checked')?"1":"0") + ($("#sat"+i).is(':checked')?"1":"0")
        		+ ($("#sun"+i).is(':checked')?"1":"0");
        	formVal["timeWeek"+i] = timeWeek;
        }
        
        
        
        
        
        $.ajaxExt({
            url: "/alarmPushSetting/setAlarmTime.htm",
            data: formVal
        }).done(function (data) {
            initRight();
            $.messager.alert("提示", "保存成功。", "info");
        });
    });
    $("#resetSetting").on("click", function () { // 重置
        initRight();
    });

    initRight();
    
    
    
    
    
});